<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="C++复习笔记, 博客">
    <meta name="description" content="个人学习储备知识与经验的秘密基地">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->

<script async src="https://www.googletagmanager.com/gtag/js?id=G-KDXT8Y4CLW"></script>
<script>
    window.dataLayer = window.dataLayer || [];
    function gtag() {
        dataLayer.push(arguments);
    }

    gtag('js', new Date());
    gtag('config', 'G-KDXT8Y4CLW');
</script>


    <title>C++复习笔记 | 不二博客</title>
    <link rel="icon" type="image/png" href="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/favicon.png">

    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/css/matery.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/css/my.css">

    <script src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 6.3.0">
<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
<link rel="alternate" href="/atom.xml" title="不二博客" type="application/atom+xml">
</head>




<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">不二博客</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">不二博客</div>
        <div class="logo-desc">
            
            个人学习储备知识与经验的秘密基地
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/weiyouwozuiku" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/weiyouwozuiku" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    
<script src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('请输入访问本文章的密码')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@src/source/_posts/PageImg/程序设计/cpp.png')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">C++复习笔记</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/Cpp/">
                                <span class="chip bg-color">Cpp</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1/" class="post-category">
                                程序设计
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2020-11-10
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2022-10-31
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    9.7k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    38 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/prism/prism.css">
        

        
        <!-- 代码块折行 -->
        <style type="text/css">
            code[class*="language-"], pre[class*="language-"] { white-space: pre-wrap !important; }
        </style>
        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <p>本文为自己复习C++时知识的补充与记录。</p>
<hr>
<p>sizeof运算符返回变量的大小。</p>
<p>逗号运算符会顺序执行一系列运算。整个逗号表达式的值是以逗号分隔的列表中的最后一个表达式的值。</p>
<p>补码范围：</p>
<table>
<thead>
<tr>
<th>数</th>
<th>8</th>
<th>16</th>
<th>32</th>
</tr>
</thead>
<tbody>
<tr>
<td><img src="https://math.now.sh?inline=UMax_w" style="filter: opacity(95%);transform:scale(0.85);text-align:center;display:inline-block;margin: 0;"></td>
<td>0xFF<br>255</td>
<td>0xFFFF<br>65535</td>
<td>0xFFFFFFFF<br>4294967295</td>
</tr>
<tr>
<td><img src="https://math.now.sh?inline=TMin_w" style="filter: opacity(95%);transform:scale(0.85);text-align:center;display:inline-block;margin: 0;"></td>
<td>0x80<br>-128</td>
<td>0x8000<br>-32768</td>
<td>0x80000000<br>-2147483648</td>
</tr>
<tr>
<td><img src="https://math.now.sh?inline=TMax_w" style="filter: opacity(95%);transform:scale(0.85);text-align:center;display:inline-block;margin: 0;"></td>
<td>0x7F<br>127</td>
<td>0x7FFF<br>32767</td>
<td>0x7FFFFFFF<br>2147483647</td>
</tr>
<tr>
<td>-1<br>0</td>
<td>0xFF<br>0x00</td>
<td>0xFFFF<br>0x0000</td>
<td>0xFFFFFFFF<br>0x00000000</td>
</tr>
</tbody>
</table>
<p><strong>对有符号数，仅不要使用右移运算，因为到底是逻辑右移还是算术右移取决于编译器。</strong></p>
<p>对于二维数组的循环，尽管可以按照列进行遍历，但是不推荐。<strong>循环时尽可能满足“空间局部性”</strong>:</p>
<ol>
<li>在一个小的时间窗口内，访问的变量地址越接近越好，这样执行速度快。</li>
<li>一般来说，需要将最长的循环放在最内层，最短的循环放在最外层，以减少CPU跨切循环层的次数。</li>
</ol>
<p><code>vector</code>是面向对象方式的动态数组。可以使用<code>capacity</code>和<code>size</code>方法来查看vector当前的容量和已经存储的元素个数。<code>insert</code>方法两个参数，第一个是位置的指针，第二个是插入的值。<code>pop_back()</code>从尾部删除，<code>push_back()</code>从尾部插入元素，<code>erase()</code>一个参数，删除元素的指针位置。<strong><code>end()</code>返回的是vector最后一个元素之后的位置，操作时注意是否-1。</strong></p>
<hr>
<h3 id="OOP">OOP<a class="anchor" href="#OOP">¶</a></h3>
<ul>
<li>封装：数据和代码捆绑在一起，避免外界干扰和不确定性访问，封装可以使得代码模块化。</li>
<li>继承：让某种类型对象获得另一个类型对象的属性和方法，继承可以扩展已存在的代码。</li>
<li>多态：同一事物表现出不同事物的能力，即向不同对象会产生不同的行为，多态的目的是为了接口重用。</li>
</ul>
<hr>
<h3 id="字符">字符<a class="anchor" href="#字符">¶</a></h3>
<p>字符常量：括在<code>单引号</code>中。如果常量以L（仅当大写）开头，则表示它是一个宽字符常量(例如L’x’)，此时必须被存储在<code>wchar_t</code>类型的变量中。否则就是一个窄字符变量。</p>
<p>字符串是以空字符<code>\0</code>结束的字符数组。</p>
<table>
<thead>
<tr>
<th style="text-align:center">二进制</th>
<th style="text-align:center">十进制</th>
<th style="text-align:center">对应字符</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">0x41</td>
<td style="text-align:center">65</td>
<td style="text-align:center">A</td>
</tr>
<tr>
<td style="text-align:center">0x61</td>
<td style="text-align:center">97</td>
<td style="text-align:center">a</td>
</tr>
<tr>
<td style="text-align:center">0x30</td>
<td style="text-align:center">48</td>
<td style="text-align:center">0</td>
</tr>
<tr>
<td style="text-align:center">0x7F</td>
<td style="text-align:center">127</td>
<td style="text-align:center">DEL</td>
</tr>
</tbody>
</table>
<p>为了表示Unicode字符集，有三种Unicode的编码方式：</p>
<ul>
<li>UTF-8：1byte来表示字符，兼容ASCII码。特点存储效率高，变长(不方便内部随机访问)，无字节序问题(可作为外部编码)</li>
<li>UTF-16：分为UTF-16BE(big endian),UTF-16LE(little endian)。特点定长(方便内部随机访问)，有字节序问题(不可作为外部编码)</li>
<li>UTF-32：分为UTF-32BE(big endian),UTF-32LE(little endian)。特点定长(方便内部随机访问)，有字节序问题(不可作为外部编码)</li>
</ul>
<h4 id="stdio">stdio<a class="anchor" href="#stdio">¶</a></h4>
<p>字符串常见操作：</p>
<ul>
<li>字符串长度:strlen(s)，不包括<code>\0</code>。</li>
<li>字符串比较:strcmp(s1,s2),相同返回0，s1&lt;s2返回值小于0，s1&gt;s2返回值大于0。按照ASCII码进行比较。</li>
<li>字符串拷贝:strcpy(s1,s2)，字符串s2复制到s1中。</li>
<li>复制指定长度字符串:strncpy(s1,s2,n)。将字符串s2中前n个字符拷贝到s1中。</li>
<li>字符串拼接:strcat(s1,s2)。将字符串s2接到s1后面。</li>
<li>查找字符串:strchr(s1,ch)。指向字符串s1中字符ch的第一次出现的位置。</li>
<li>查找字符串strstr(s1,s2)。指向字符串s2的第一次出现的位置。</li>
</ul>
<p>推荐使用strnlen_s,strcpy_s,strncpy_s,strcat_s等API函数，更安全。</p>
<p>在进行底层代码安全性测试的时候需要加上<code>CRT_SECURE_NO_WARNINGS</code>让编译器忽略错误。</p>
<h4 id="string">string<a class="anchor" href="#string">¶</a></h4>
<p>获得字符串长度：<code>s.length</code>或<code>s.size()</code>二者等价。<code>s.capacity()</code>查看字符串总空间。</p>
<p>转换为C风格的字符串使用如下代码：</p>
<pre class="line-numbers language-Cpp" data-language="Cpp"><code class="language-Cpp">const char *c_str = s1.c_str();<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p><code>c_str()</code>生成一个const char*指针，指向以空字符终止的数组。</p>
<hr>
<h3 id="指针">指针<a class="anchor" href="#指针">¶</a></h3>
<p><code>const</code>和<code>*</code>修饰的部分：1.先看左侧最近的部分;2.如果左侧没有，则看右侧。</p>
<p><code>*</code>操作符具有从右往左的结合性。<code>**</code>这个表达式相当于<code>*(*c)</code>，必须从里向外逐层求值。</p>
<h4 id="野指针">野指针<a class="anchor" href="#野指针">¶</a></h4>
<p>直接使用未初始化或非法指针，可能发生两种情况：<strong>1.定位到非法地址，程序出错，从而终止。2.定位到一个可以访问的地址，无意修改了它，这样的错误难以捕捉，引发的错误可能与原先用于操作的代码完全不相干！</strong></p>
<p>用指针进行间接访问之前，一定要非常小心，<strong>确保它已经初始化，并被恰当的赋值。</strong></p>
<p>对于一个指针，如果已经知道将被初始化为什么地址，那么请赋值给它，否则请将它<strong>设置为NULL</strong>。</p>
<p>在对一个指针进行间接引用前，请先判断这个指针的值是否为NULL。</p>
<p>野指针是指向"垃圾"内存的指针。if判断对它们不起作用，因为没有置NULL。一般有三种情况：</p>
<ol>
<li>指针变量没有初始化;</li>
<li>已经释放不用的指针没有置NULL,如delete和free之后的指针；</li>
<li>指针操作超越了变量的作用范围;</li>
</ol>
<p>没有初始化的，不用的或者超出范围的指针请置为NULL。</p>
<p>编译器程序分解成符号的方法是：一个字符一个字符的读入，一直到读入的字符不再能组成一个有意义的符号。这个处理过程称为"贪心法"。</p>
<p>栈内分配内存从高地址向低地址分配，堆区分配内存是从低地址到高地址。</p>
<p><img src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@src/source/_posts/%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1/Cpp/Cpp%E5%A4%8D%E4%B9%A0%E7%AC%94%E8%AE%B0/Cpp%E5%A4%8D%E4%B9%A0%E7%AC%94%E8%AE%B0_Cpp%E5%AD%98%E5%82%A8%E7%BB%93%E6%9E%84.png" alt="Cpp复习笔记_Cpp存储结构.png"></p>
<p>主流的编程语言中，C++是唯一一个依赖RAII(Resource Acquisition Is Initialization)来做资源管理的。</p>
<p>RAII依托栈和析构函数，来对所有资源——包括堆内存在内进行管理。对RAII的使用，使得C<ins>不需要类似于Java那样的垃圾收集方法，也能有效的对内存进行管理。RAII的存在，也是垃圾收集虽然理论上可以在C</ins>使用，但从来没有真正流行的原因。</p>
<p>RAII有些比较成熟的智能指针代表：如<code>std:auto_ptr</code>和<code>boost:shared_ptr</code>。</p>
<p>栈和堆中变量对比：</p>
<table>
<thead>
<tr>
<th></th>
<th>stack</th>
<th>heap</th>
</tr>
</thead>
<tbody>
<tr>
<td>作用域</td>
<td>函数体内，语句块{}作用域</td>
<td>整个程序范围内，由new,malloc开始，delete,free结束</td>
</tr>
<tr>
<td>编译期大小确定</td>
<td>变量大小范围确定</td>
<td>变量大小范围不确定，需要在运行期确定</td>
</tr>
<tr>
<td>大小范围</td>
<td>Windows系统默认栈大小是1M,linux常见的默认的栈大小是8M或10M(通过ulimit -s查看，不同的linux发行版本不保证相同)Manjaro就是8M</td>
<td>所有系统的堆空间上限是接近内存(虚拟内存)的总大小的(一部分被OS占用)</td>
</tr>
<tr>
<td>内存分配方式</td>
<td>地址由高到低减少</td>
<td>地址由低到高增加</td>
</tr>
<tr>
<td>内容是否可变</td>
<td>可变</td>
<td>可变</td>
</tr>
</tbody>
</table>
<p>全局静态存储区和常量存储区的变量对比：</p>
<table>
<thead>
<tr>
<th></th>
<th>全局静态存储区</th>
<th>常量存储区</th>
</tr>
</thead>
<tbody>
<tr>
<td>存储内容</td>
<td>全局变量</td>
<td>常量</td>
</tr>
<tr>
<td>编译期间大小是否确定</td>
<td>确定</td>
<td>确定</td>
</tr>
<tr>
<td>内容是否可变</td>
<td>可变</td>
<td>不可变</td>
</tr>
</tbody>
</table>
<h4 id="内存泄漏">内存泄漏<a class="anchor" href="#内存泄漏">¶</a></h4>
<h5 id="定义">定义<a class="anchor" href="#定义">¶</a></h5>
<p>内存泄漏指程序中已动态分配的堆内存由于某种原因程序未释放或无法释放造成系统内存的浪费，导致程序运行速度减慢甚至系统崩溃等严重后果。</p>
<h5 id="发生原因和排查方式">发生原因和排查方式<a class="anchor" href="#发生原因和排查方式">¶</a></h5>
<ol>
<li>内存泄漏主要发生在堆内存分配方式中，即“配置了内存后，所有指向该内存的指针都遗失了”。若缺乏语言这样的gc机制，这样的内存片就无法归还系统。</li>
<li>因为内存泄漏属于程序运行中的问题，无法通过编译识别，所以只能在程序运行过程中来识别和诊断。</li>
</ol>
<hr>
<h3 id="智能指针">智能指针<a class="anchor" href="#智能指针">¶</a></h3>
<p><strong>C++11之后对空指针的判断推荐使用<code>nullter</code>，避免<code>NULL</code>的二义性。</strong></p>
<p><code>unique_ptr</code>、<code>shared_ptr</code>、<code>weak_ptr</code>和C<ins>11中已经废弃的<code>auto_ptr</code>，在C</ins>17中被正式删除。需要导入头文件<code>&lt;memory&gt;</code></p>
<h4 id="auto-ptr-不推荐使用">auto_ptr (不推荐使用)<a class="anchor" href="#auto-ptr-不推荐使用">¶</a></h4>
<p>由<code>new expression</code>获得对象，在<code>auto_ptr</code>对象销毁时，他所管理的对象也会自动被delete掉。</p>
<p>所有权转移：不小心把它传递给另外的智能指针，原本的指针就不再拥有这个对象了。在拷贝/赋值过程中，会直接剥夺指针对原对象对内存的控制权，转交给新对象，然后再将原对象指针置为<code>nullptr</code>。</p>
<p><strong>当拷贝/赋值时发生所有权转移，再次访问原对象时程序就会报错。</strong></p>
<h4 id="unique-ptr">unique_ptr<a class="anchor" href="#unique-ptr">¶</a></h4>
<p><code>unique_ptr</code>是专属所有权，所以<code>uniqure_ptr</code>管理的内存，只能被一个对象持有，不支持复制和赋值。</p>
<p>移动语义：<code>unique_ptr</code>禁止了拷贝语义，但有时我们也需要能够转移所有权，于是提供了移动语义，即可以使用std::move()进行控制所有权的转移。</p>
<p>语法: <code>std::make_unique&lt;int&gt;(10)</code>或<code>unique_ptr&lt;int&gt;(new int(10))</code></p>
<h4 id="shared-ptr">shared_ptr<a class="anchor" href="#shared-ptr">¶</a></h4>
<p><code>shared_ptr</code>通过一个引用计数共享一个对象，代表的是共享所有权，即多个<code>shared_ptr</code>可以共享同一块内存。</p>
<p><code>shared_ptr</code>是为了解决<code>auto_ptr</code>在对象所有权上的局限性，在使用引用计数的机制上提供了可以共享所有权的智能指针，当然这需要额外的开销。当引用计数为0时，该对象没有被使用，可以进行析构。</p>
<p>但是可能存在一些问题：</p>
<ul>
<li>
<p>循环引用:引用计数会带来循环引用的问题</p>
<p>循环引用会导致堆里的内存无法被正常回收，造成内存泄漏。</p>
</li>
</ul>
<p><img src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@src/source/_posts/%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1/Cpp/Cpp%E5%A4%8D%E4%B9%A0%E7%AC%94%E8%AE%B0/Cpp%E5%A4%8D%E4%B9%A0%E7%AC%94%E8%AE%B0_%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88%E7%9A%84%E5%BE%AA%E7%8E%AF%E5%BC%95%E7%94%A8%E9%97%AE%E9%A2%98.png" alt="Cpp复习笔记_智能指针的循环引用问题.png"></p>
<h4 id="weak-ptr">weak_ptr<a class="anchor" href="#weak-ptr">¶</a></h4>
<p><code>weak_ptr</code>被设计为与<code>shared_ptr</code>共同工作，用一种观察者模式工作。</p>
<p>作用是协助<code>shared_ptr</code>工作，可获得资源的观测权，像旁观者那样观测资源的使用情况。观察者意味着<code>weak_ptr</code>只对<code>shared_ptr</code>进行引用，而不改变其引用计数，当被观察的<code>shared_ptr</code>失效后，相应的<code>weak_ptr</code>也相应失效。</p>
<hr>
<h3 id="引用">引用<a class="anchor" href="#引用">¶</a></h3>
<p>使用引用的优点：</p>
<ol>
<li>不存在空引用，避免了空指针的出现。</li>
<li>必须初始化，避免了野指针的出现。</li>
<li>一个引用必须永远指向它初始化的那个对象</li>
</ol>
<p>**有了指针为什么还需要引用？**为了支持函数运算符重载。</p>
<p>**有了引用为什么还需要指针?**为了兼容C语言。</p>
<p>左值引用：&lt;类型&gt;&amp; &lt;变量名&gt;，定义的时候就需要进行初始化。</p>
<p>右值引用：&lt;类型&gt;&amp;&amp; &lt;变量名&gt;</p>
<hr>
<h3 id="自定义结构">自定义结构<a class="anchor" href="#自定义结构">¶</a></h3>
<h4 id="枚举">枚举<a class="anchor" href="#枚举">¶</a></h4>
<p>使用<code>#define</code>和<code>const</code>创建符号变量，使用<code>enum</code>不仅能够创建符号常量，还能定义新的数据类型。</p>
<p>枚举类型enum(enumeration)的声明和定义：</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">enum</span> <span class="token class-name">wT</span><span class="token punctuation">{</span>Monday<span class="token punctuation">,</span>Tuesday<span class="token punctuation">,</span>Wednesday<span class="token punctuation">,</span>Thursday<span class="token punctuation">,</span>Friday<span class="token punctuation">,</span>Saturday<span class="token punctuation">,</span>Sunday<span class="token punctuation">}</span><span class="token punctuation">;</span>
wT weekday<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<h4 id="结构体">结构体<a class="anchor" href="#结构体">¶</a></h4>
<p>使用<code>struct</code>定义</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">strcut Student<span class="token punctuation">{</span>
    <span class="token keyword">char</span> name<span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> age<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//让对象实例成为一个callable,能够作为std::function</span>
<span class="token keyword">struct</span> <span class="token class-name">Display</span> <span class="token punctuation">{</span>
    <span class="token keyword">void</span> <span class="token keyword">operator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">int</span> <span class="token operator">&amp;</span>n<span class="token punctuation">)</span> <span class="token keyword">const</span> <span class="token punctuation">{</span>
        cout <span class="token operator">&lt;&lt;</span> n <span class="token operator">&lt;&lt;</span> <span class="token string">" "</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token comment">//调用可使用</span>
Display<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//打印1</span>
<span class="token comment">//或者</span>
<span class="token function">for_each</span><span class="token punctuation">(</span>begin<span class="token punctuation">,</span>end<span class="token punctuation">,</span><span class="token function">Display</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><strong>结构体的内存布局需要综合考虑CPU的位数和当前结构体中最大的类型所占空间。不同的结构体中数据的排序也会导致结构体占用空间的不同。结构体所占的空间必然是最大元素所占空间的整数倍。</strong></p>
<p>结构体中数据——缺省对齐原则</p>
<p>针对32位CPU：</p>
<ul>
<li>char：任何地址</li>
<li>short：偶数地址</li>
<li>int：4的整数倍地址</li>
<li>double ：8的整数倍地址</li>
</ul>
<p>修改默认编译选项：</p>
<ul>
<li>
<p>Visual C++:</p>
<p>#pragma pack(n)</p>
</li>
<li>
<p>g++:</p>
<p>_attribute_(aligned(n))</p>
<p>_attribute_(_packed_)</p>
</li>
</ul>
<p><strong>此处的n表示是n的倍数进行分配。</strong></p>
<p><strong>推荐把小的元素分配在一起！！！！</strong></p>
<h4 id="联合体">联合体<a class="anchor" href="#联合体">¶</a></h4>
<p>使用<code>union</code>定义</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">union</span> Score<span class="token punctuation">{</span>
    <span class="token keyword">double</span> sc<span class="token punctuation">;</span>
    <span class="token keyword">char</span> level<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<p><strong>联合体的空间按照最大元素所占空间决定。</strong></p>
<h4 id="宏定义">宏定义<a class="anchor" href="#宏定义">¶</a></h4>
<p>宏定义版min函数：<code>#define MIN(a,b) (((a)&gt;=(b))?(b):(a))</code>，这里的a或b可能是式子，所以加上括号。</p>
<hr>
<h3 id="泛型">泛型<a class="anchor" href="#泛型">¶</a></h3>
<ul>
<li>如果说面向对象是一种通过间接层来调用函数，以换取一种抽象，那么泛型编程则是更直接的抽象，因为它不会因为间接层而损失效率；</li>
<li>不同于面向对象的动态期多态，泛型编程则是一种静态期多态，通过编译器生成最直接的代码；</li>
<li>泛型编程可以将算法与特定类型，结构相剥离，尽可能复用代码；</li>
</ul>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">T</span><span class="token operator">&gt;</span>
T <span class="token function">max</span><span class="token punctuation">(</span>T a<span class="token punctuation">,</span> T b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> a <span class="token operator">&gt;</span> b <span class="token operator">?</span> a <span class="token operator">:</span> b<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">//特化</span>
<span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token operator">&gt;</span>
<span class="token keyword">char</span> <span class="token operator">*</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>a<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span>b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token function">strcmp</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">//简单的for_each实现</span>
<span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token punctuation">,</span><span class="token keyword">typename</span> <span class="token class-name">Func</span><span class="token operator">&gt;</span>
<span class="token keyword">void</span> <span class="token function">for_each</span><span class="token punctuation">(</span>T<span class="token operator">*</span> begin<span class="token punctuation">,</span>T<span class="token operator">*</span> end<span class="token punctuation">,</span><span class="token keyword">const</span> Func<span class="token operator">&amp;</span> f<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>begin<span class="token operator">!=</span>end<span class="token punctuation">)</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token operator">*</span>begin<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="模板元编程">模板元编程<a class="anchor" href="#模板元编程">¶</a></h4>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">int</span> n<span class="token operator">&gt;</span>
<span class="token keyword">struct</span> <span class="token class-name">Sum</span> <span class="token punctuation">{</span>
    <span class="token keyword">enum</span> <span class="token class-name">Value</span> <span class="token punctuation">{</span>
        N <span class="token operator">=</span> Sum<span class="token operator">&lt;</span>n <span class="token operator">-</span> <span class="token number">1</span><span class="token operator">&gt;</span><span class="token double-colon punctuation">::</span>N <span class="token operator">+</span> n
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token operator">&gt;</span>
<span class="token keyword">struct</span> <span class="token class-name">Sum</span><span class="token operator">&lt;</span><span class="token number">1</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">enum</span> <span class="token class-name">Value</span> <span class="token punctuation">{</span>
        N <span class="token operator">=</span> <span class="token number">1</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    cout <span class="token operator">&lt;&lt;</span> Sum<span class="token operator">&lt;</span><span class="token number">100</span><span class="token operator">&gt;</span><span class="token double-colon punctuation">::</span>N<span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>这个函数的整个计算过程不是在运行时完成，而是在编译期完成。</p>
<hr>
<h3 id="函数">函数<a class="anchor" href="#函数">¶</a></h3>
<p>函数名与参数列表一起构成了函数签名。</p>
<p>函数的参数默认值必须是最右的有值才行，这取决于代码编译成汇编语言时先操作的是最右的参数进行压栈，之后从右向左进行赋值。</p>
<h4 id="判断">判断<a class="anchor" href="#判断">¶</a></h4>
<p>switch分支与if分支比较：</p>
<p>使用场景：</p>
<ol>
<li>switch只支持常量值固定相等的分支判断;</li>
<li>if还可以判断区间范围;</li>
<li>用switch能做的，用if都能做，但反过来则不行;</li>
</ol>
<p>性能比较：</p>
<ol>
<li>分支少时，差别不是很大;分支多时，switch性能较高;</li>
<li>if开始几个分支效果高，之后效率递减;</li>
<li>switch所有case的速度几乎一样;</li>
</ol>
<h4 id="循环">循环<a class="anchor" href="#循环">¶</a></h4>
<p>从汇编的角度来看，<code>do while</code>效率高于<code>while</code>更高于<code>for</code>。</p>
<h4 id="函数指针">函数指针<a class="anchor" href="#函数指针">¶</a></h4>
<p>指向函数入口地址的指针就是函数指针。</p>
<p>一般形式：<code>数据类型 (*指针变量名)(参数表)</code></p>
<p>区分：</p>
<p>​	<code>int(*p)int</code>//是指针，指向一个函数入口地址</p>
<p>​	<code>int* p(int)</code>//是函数，返回的值是一个指针</p>
<h4 id="命名空间">命名空间<a class="anchor" href="#命名空间">¶</a></h4>
<p>命名空间可作为附加信息来区分不同库中相同名称的函数、类、变量等，命名空间即定义了上下文。本质上，命名空间就是定义了一个范围。</p>
<p>用法:</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">//定义函数时</span>
<span class="token keyword">namespace</span> <span class="token operator">&lt;</span>自定义空间名<span class="token operator">&gt;</span><span class="token punctuation">{</span>
    <span class="token comment">//定义函数</span>
<span class="token punctuation">}</span>
<span class="token comment">//函数实现时</span>
<span class="token keyword">namespace</span> <span class="token operator">&lt;</span>自定义空间名<span class="token operator">&gt;</span><span class="token punctuation">{</span>
    <span class="token comment">//实现函数 </span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="内联函数">内联函数<a class="anchor" href="#内联函数">¶</a></h4>
<p>内联函数在编译时，编译器会把该函数的代码副本放置在每个调用该函数的地方。</p>
<p>引入内联函数的目的是解决程序中函数调用的效率问题。<strong>内联函数内部不能有太复杂的逻辑，编译器有时会有自己的优化策略，所以内联不一定起作用。</strong></p>
<p>用法：<code>inline &lt;函数定义&gt;</code></p>
<h4 id="递归">递归<a class="anchor" href="#递归">¶</a></h4>
<p>缺陷：</p>
<ul>
<li>空间上需要开辟大量的栈空间</li>
<li>时间上可能需要有大量重复运算</li>
</ul>
<p>优化：</p>
<ul>
<li>尾递归：所有递归形式的调用都出现在函数的末尾</li>
<li>使用循环代替</li>
<li>使用动态规划，空间换时间</li>
</ul>
<hr>
<h3 id="类">类<a class="anchor" href="#类">¶</a></h3>
<p>C++使用<code>struct</code>、<code>class</code>来定义一个类。struct默认成员权限是public，class默认成员权限是private。</p>
<p><strong>析构函数前面需要加上<code>virtual</code></strong>。</p>
<p>子类方法实现不一致时，父类的该方法前面需要加上<code>virtual</code>。<strong>这里也是多态的体现</strong>。</p>
<p>子类继承父类形式：<code>class &lt;子类&gt; :&lt;权限修饰符&gt; &lt;类名&gt;</code>。</p>
<p>类定义和实现的简单展示：</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">//Complex.h</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">ACWING_COMPLEX_H</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">ACWING_COMPLEX_H</span></span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>

<span class="token keyword">class</span> <span class="token class-name">Complex</span> <span class="token punctuation">{</span>

<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token function">Complex</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">Complex</span><span class="token punctuation">(</span><span class="token keyword">double</span> real<span class="token punctuation">,</span> <span class="token keyword">double</span> image<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 构造函数</span>
    <span class="token keyword">virtual</span> <span class="token operator">~</span><span class="token function">Complex</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//析构函数</span>
    <span class="token keyword">double</span> <span class="token function">getReal</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">const</span><span class="token punctuation">;</span>

    <span class="token keyword">double</span> <span class="token function">getImage</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">const</span><span class="token punctuation">;</span>

    <span class="token keyword">void</span> <span class="token function">setReal</span><span class="token punctuation">(</span><span class="token keyword">double</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">void</span> <span class="token function">setImage</span><span class="token punctuation">(</span><span class="token keyword">double</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>

    Complex <span class="token keyword">operator</span><span class="token operator">+</span><span class="token punctuation">(</span><span class="token keyword">const</span> Complex <span class="token operator">&amp;</span>x<span class="token punctuation">)</span> <span class="token keyword">const</span><span class="token punctuation">;</span><span class="token comment">//+运算符重载</span>
    Complex <span class="token operator">&amp;</span><span class="token keyword">operator</span><span class="token operator">+=</span><span class="token punctuation">(</span><span class="token keyword">const</span> Complex <span class="token operator">&amp;</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>

    Complex <span class="token operator">&amp;</span><span class="token keyword">operator</span><span class="token operator">=</span><span class="token punctuation">(</span><span class="token keyword">const</span> Complex <span class="token operator">&amp;</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//=运算符重载</span>
    <span class="token function">Complex</span><span class="token punctuation">(</span><span class="token keyword">const</span> Complex <span class="token operator">&amp;</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//拷贝构造</span>
    <span class="token comment">//前置和后置++</span>
    <span class="token comment">//前置++</span>
    Complex <span class="token operator">&amp;</span><span class="token keyword">operator</span><span class="token operator">++</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//后置++</span>
    Complex <span class="token keyword">operator</span><span class="token operator">++</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//前置--</span>
    Complex <span class="token operator">&amp;</span><span class="token keyword">operator</span><span class="token operator">--</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//后置--</span>
    Complex <span class="token keyword">operator</span><span class="token operator">--</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//等于</span>
    <span class="token keyword">bool</span> <span class="token keyword">operator</span><span class="token operator">==</span><span class="token punctuation">(</span><span class="token keyword">const</span> Complex <span class="token operator">&amp;</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//不等于</span>
    <span class="token keyword">bool</span> <span class="token keyword">operator</span><span class="token operator">!=</span><span class="token punctuation">(</span><span class="token keyword">const</span> Complex <span class="token operator">&amp;</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">protected</span><span class="token operator">:</span>
    <span class="token comment">//输入输出运算符</span>
    <span class="token keyword">friend</span> std<span class="token double-colon punctuation">::</span>ostream <span class="token operator">&amp;</span><span class="token keyword">operator</span><span class="token operator">&lt;&lt;</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>ostream <span class="token operator">&amp;</span>os<span class="token punctuation">,</span> <span class="token keyword">const</span> Complex <span class="token operator">&amp;</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">friend</span> std<span class="token double-colon punctuation">::</span>istream <span class="token operator">&amp;</span><span class="token keyword">operator</span><span class="token operator">&gt;&gt;</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>istream <span class="token operator">&amp;</span>is<span class="token punctuation">,</span> Complex <span class="token operator">&amp;</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">private</span><span class="token operator">:</span>
    <span class="token keyword">double</span> _real<span class="token punctuation">;</span>
    <span class="token keyword">double</span> _image<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span> <span class="token comment">//ACWING_COMPLEX_H</span></span>

<span class="token comment">//Complex.cpp</span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">"Complex.h"</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>

<span class="token class-name">Complex</span><span class="token double-colon punctuation">::</span><span class="token function">Complex</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token function">_real</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">_image</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

<span class="token class-name">Complex</span><span class="token double-colon punctuation">::</span><span class="token function">Complex</span><span class="token punctuation">(</span><span class="token keyword">double</span> real<span class="token punctuation">,</span> <span class="token keyword">double</span> image<span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token function">_real</span><span class="token punctuation">(</span>real<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">_image</span><span class="token punctuation">(</span>image<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

<span class="token class-name">Complex</span><span class="token double-colon punctuation">::</span><span class="token operator">~</span><span class="token function">Complex</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

<span class="token punctuation">}</span>

<span class="token keyword">double</span> <span class="token class-name">Complex</span><span class="token double-colon punctuation">::</span><span class="token function">getReal</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">const</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> _real<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">double</span> <span class="token class-name">Complex</span><span class="token double-colon punctuation">::</span><span class="token function">getImage</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">const</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> _image<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">void</span> <span class="token class-name">Complex</span><span class="token double-colon punctuation">::</span><span class="token function">setReal</span><span class="token punctuation">(</span><span class="token keyword">double</span> r<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    _real <span class="token operator">=</span> r<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">void</span> <span class="token class-name">Complex</span><span class="token double-colon punctuation">::</span><span class="token function">setImage</span><span class="token punctuation">(</span><span class="token keyword">double</span> i<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    _image <span class="token operator">=</span> i<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

Complex Complex<span class="token double-colon punctuation">::</span><span class="token keyword">operator</span><span class="token operator">+</span><span class="token punctuation">(</span><span class="token keyword">const</span> Complex <span class="token operator">&amp;</span>x<span class="token punctuation">)</span> <span class="token keyword">const</span> <span class="token punctuation">{</span>
    <span class="token comment">//避免临时对象的产生，因为定义一个新的临时对象在返回的时候会触发拷贝构造函数</span>
    <span class="token keyword">return</span> <span class="token function">Complex</span><span class="token punctuation">(</span>x<span class="token punctuation">.</span><span class="token function">getReal</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> _real<span class="token punctuation">,</span> x<span class="token punctuation">.</span><span class="token function">getImage</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> _image<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

Complex <span class="token operator">&amp;</span>Complex<span class="token double-colon punctuation">::</span><span class="token keyword">operator</span><span class="token operator">+=</span><span class="token punctuation">(</span><span class="token keyword">const</span> Complex <span class="token operator">&amp;</span>x<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    _real <span class="token operator">+=</span> x<span class="token punctuation">.</span><span class="token function">getReal</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    _image <span class="token operator">+=</span> x<span class="token punctuation">.</span><span class="token function">getImage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">/**
     * 上面两行也可以使用如下代码执行
     * _real+=x._real;
     * _image+=x._image;
     * 在编译器遇到_real和_image变量时，将会在其所属对象x所属的类域中查找该符号。
     * 既然查找的是类域，由于+=函数属于Complex类，+=函数可以访问Complex类中所有变量，所以可以直接访问私有变量。
     */</span>
    <span class="token keyword">return</span> <span class="token operator">*</span><span class="token keyword">this</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

Complex <span class="token operator">&amp;</span>Complex<span class="token double-colon punctuation">::</span><span class="token keyword">operator</span><span class="token operator">=</span><span class="token punctuation">(</span><span class="token keyword">const</span> Complex <span class="token operator">&amp;</span>x<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">//检查自赋值</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span> <span class="token operator">!=</span> <span class="token operator">&amp;</span>x<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        _real <span class="token operator">=</span> x<span class="token punctuation">.</span><span class="token function">getReal</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        _image <span class="token operator">=</span> x<span class="token punctuation">.</span><span class="token function">getImage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token operator">*</span><span class="token keyword">this</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token class-name">Complex</span><span class="token double-colon punctuation">::</span><span class="token function">Complex</span><span class="token punctuation">(</span><span class="token keyword">const</span> Complex <span class="token operator">&amp;</span>x<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    _real <span class="token operator">=</span> x<span class="token punctuation">.</span><span class="token function">getReal</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    _image <span class="token operator">=</span> x<span class="token punctuation">.</span><span class="token function">getImage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

Complex <span class="token operator">&amp;</span>Complex<span class="token double-colon punctuation">::</span><span class="token keyword">operator</span><span class="token operator">++</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    _real<span class="token operator">++</span><span class="token punctuation">;</span>
    _image<span class="token operator">++</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token operator">*</span><span class="token keyword">this</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

Complex Complex<span class="token double-colon punctuation">::</span><span class="token keyword">operator</span><span class="token operator">++</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">Complex</span><span class="token punctuation">(</span>_real<span class="token operator">++</span><span class="token punctuation">,</span> _image<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

Complex <span class="token operator">&amp;</span>Complex<span class="token double-colon punctuation">::</span><span class="token keyword">operator</span><span class="token operator">--</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    _real<span class="token operator">--</span><span class="token punctuation">;</span>
    _image<span class="token operator">--</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token operator">*</span><span class="token keyword">this</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

Complex Complex<span class="token double-colon punctuation">::</span><span class="token keyword">operator</span><span class="token operator">--</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">Complex</span><span class="token punctuation">(</span>_real<span class="token operator">--</span><span class="token punctuation">,</span> _image<span class="token operator">--</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">bool</span> Complex<span class="token double-colon punctuation">::</span><span class="token keyword">operator</span><span class="token operator">==</span><span class="token punctuation">(</span><span class="token keyword">const</span> Complex <span class="token operator">&amp;</span>x<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span>x<span class="token punctuation">.</span><span class="token function">getReal</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> _real<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span>x<span class="token punctuation">.</span><span class="token function">getImage</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> _image<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">bool</span> Complex<span class="token double-colon punctuation">::</span><span class="token keyword">operator</span><span class="token operator">!=</span><span class="token punctuation">(</span><span class="token keyword">const</span> Complex <span class="token operator">&amp;</span>x<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token operator">!</span><span class="token punctuation">(</span><span class="token punctuation">(</span>x<span class="token punctuation">.</span><span class="token function">getReal</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> _real<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span>x<span class="token punctuation">.</span><span class="token function">getImage</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> _image<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

std<span class="token double-colon punctuation">::</span>ostream <span class="token operator">&amp;</span><span class="token keyword">operator</span><span class="token operator">&lt;&lt;</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>ostream <span class="token operator">&amp;</span>os<span class="token punctuation">,</span> <span class="token keyword">const</span> Complex <span class="token operator">&amp;</span>x<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    os <span class="token operator">&lt;&lt;</span> <span class="token string">"real value is "</span> <span class="token operator">&lt;&lt;</span> x<span class="token punctuation">.</span>_real <span class="token operator">&lt;&lt;</span> <span class="token string">",image value is "</span> <span class="token operator">&lt;&lt;</span> x<span class="token punctuation">.</span>_image<span class="token punctuation">;</span>
    <span class="token keyword">return</span> os<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

std<span class="token double-colon punctuation">::</span>istream <span class="token operator">&amp;</span><span class="token keyword">operator</span><span class="token operator">&gt;&gt;</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>istream <span class="token operator">&amp;</span>is<span class="token punctuation">,</span> Complex <span class="token operator">&amp;</span>x<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    is <span class="token operator">&gt;&gt;</span> x<span class="token punctuation">.</span>_real <span class="token operator">&gt;&gt;</span> x<span class="token punctuation">.</span>_image<span class="token punctuation">;</span>
    <span class="token keyword">return</span> is<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><strong>成员函数的最后加上<code>const</code>表示该函数的权限为只读</strong>。</p>
<p>当自定义了有参的构造函数时，默认的构造函数会消失，需要自己手动添加。</p>
<p><strong>尽量减少函数中临时对象的使用。如果可以在函数返回时利用新的数据直接返回对象，这样可以有效的避免系统调用拷贝构造函数。</strong></p>
<p>**前置<ins>的运算符重载的声明为<code>&lt;类型名&gt;&amp; operaotr++ ()</code>，后置</ins>的运算符重载的声明为<code>&lt;类型名&gt; operator++ (int)</code>，**前置–和后置–类似。</p>
<p><strong>封装是编译期的概念，是针对类型而非对象，在类的成员函数中可以访问同类型实例对象的私有成员变量。</strong></p>
<h4 id="头文件的重复包含问题">头文件的重复包含问题<a class="anchor" href="#头文件的重复包含问题">¶</a></h4>
<p>为了避免同一个文件被include多次，有两种方式：</p>
<ol>
<li>
<p>使用宏来防止同一文件被多次包含；</p>
<p>优点：可移植性好。</p>
<p>缺点：无法防止宏名重复，难以派错。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">_SOMEFILE_H_</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">_SOMEFILE_H_</span></span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
</li>
<li>
<p>使用编译器来防止同一文件被多次包含；</p>
<p>优点：可以防止宏名重复，易排错。</p>
<p>缺点：可移植性不好。</p>
<p><code>#pragma once</code></p>
</li>
</ol>
<h4 id="抽象类与接口">抽象类与接口<a class="anchor" href="#抽象类与接口">¶</a></h4>
<p>接口描述了类的行为和功能能，而不需要完成类的特定实现。</p>
<p>C++接口是通过使用抽象类来实现的，抽象类与数据抽象互不混淆，数据抽象是一个把实现细节与相关数据分离开的概念。</p>
<p>如果类中至少有一个函数被声明为纯虚函数，则这个类就是抽象类。纯虚函数是通过在声明中使用<code>virtual &lt;函数定义&gt;=0</code>来指定。</p>
<p>抽象类不能被用于实例化对象，它只能作为接口使用，如果试图实例化一个抽象类的对象，会导致编译错误。</p>
<h5 id="虚函数">虚函数<a class="anchor" href="#虚函数">¶</a></h5>
<p>类的虚函数通过虚表的方式和类的成员变量组合在一起，寄存在类的对象中，但是类的成员函数并不在其中。而是通过this指针与对象相结合。this指针一般存储在<code>ecx</code>寄存器中。</p>
<hr>
<h3 id="I-O流">I/O流<a class="anchor" href="#I-O流">¶</a></h3>
<p>传统的C中I/O有<code>printf,scanf,getch,gets</code>等函数，它们的问题是：</p>
<ul>
<li>不可编程，仅仅能识别固有的数据类型;</li>
<li>代码的可移植性差;</li>
</ul>
<p>C++中的I/O流<code>istream,ostream</code>等：</p>
<ul>
<li>可编程，对于类库的设计者来说很有用;</li>
<li>简化编程，能使得I/O的风格一致;</li>
</ul>
<pre class="mermaid">graph LR
ios--&gt;istream;
ios--&gt;ostream;
istream--&gt;ifstream &amp; istrstream &amp; iostream
ostream--&gt;iostream &amp; ostrstream &amp; ofstream
iostream--&gt;fstream
streambuf--&gt;strstreambuf &amp; filebuf</pre>
<p><img src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@src/source/_posts/%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1/Cpp/Cpp%E5%A4%8D%E4%B9%A0%E7%AC%94%E8%AE%B0/Cpp%E5%A4%8D%E4%B9%A0%E7%AC%94%E8%AE%B0_IO%E6%B5%81%E7%B1%BB%E5%B1%82%E6%AC%A1%E7%BB%93%E6%9E%84%E5%9B%BE.png" alt="Cpp复习笔记_IO流类层次结构图.png"></p>
<h4 id="缓存区">缓存区<a class="anchor" href="#缓存区">¶</a></h4>
<p>标准IO提供的三种类型的缓存模式：</p>
<ul>
<li>按块缓存：如文件系统</li>
<li>按行缓存：\n</li>
<li>不缓存</li>
</ul>
<p><strong>使用<code>cin.ignore(&lt;长度&gt;,&lt;作为结束的标识符&gt;)</code>清空输入的脏数据。这里的长度可以设置为<code>std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(),'\n');</code>，可能需要添加头文件<code>#include &lt;limits&gt;</code>。</strong></p>
<h4 id="文件操作">文件操作<a class="anchor" href="#文件操作">¶</a></h4>
<p>C++把每个文件都看成是一个有序的字节序列，每个文件都以文件结束标志结束。</p>
<p>按照文件中数据的组织形式可把文件分成为：</p>
<ul>
<li>
<p>文本文件：文件中信息形式为ASCII码文件，每个字符占一个字节。</p>
</li>
<li>
<p>二进制文件：文件中信息的形式与其在内存中的形式相同。</p>
</li>
</ul>
<p>文件操作步骤：</p>
<ol>
<li>打开文件用于读和写open;</li>
<li>检查打开是否成功fail;</li>
<li>读或者写read,write;</li>
<li>检查是否读完<code>EOF(end of file)</code>;</li>
<li>使用完文件后关闭文件close;</li>
</ol>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;fstream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token keyword">void</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    std<span class="token double-colon punctuation">::</span>fstream fout<span class="token punctuation">;</span>
    fout<span class="token punctuation">.</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">"&lt;文件路径&gt;"</span><span class="token punctuation">,</span><span class="token operator">&lt;</span>操作模式<span class="token operator">&gt;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>fout<span class="token punctuation">.</span><span class="token function">fail</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        std<span class="token double-colon punctuation">::</span>cout<span class="token operator">&lt;&lt;</span><span class="token string">"failed!"</span><span class="token operator">&lt;&lt;</span>std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    fout<span class="token operator">&lt;&lt;</span><span class="token string">"&lt;文字&gt;"</span><span class="token operator">&lt;&lt;</span>std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>文件的方法方式：</p>
<table>
<thead>
<tr>
<th>方式</th>
<th>作用</th>
</tr>
</thead>
<tbody>
<tr>
<td>ios::in</td>
<td>打开文件进行读操作(ifstream默认模式)</td>
</tr>
<tr>
<td>ios::out</td>
<td>打开文件进行写操作(ofstream默认模式)</td>
</tr>
<tr>
<td>ios::ate</td>
<td>打开一个已有输入或输出文件并查找到文件尾</td>
</tr>
<tr>
<td>ios::app</td>
<td>打开文件以便在文件的尾部添加数据</td>
</tr>
<tr>
<td>ios::nocreate</td>
<td>如果文件不存在，则打开操作失败</td>
</tr>
<tr>
<td>ios::trunc</td>
<td>如果文件存在，清除文件原有内容（默认）</td>
</tr>
<tr>
<td>ios::binary</td>
<td>以二进制方式打开</td>
</tr>
</tbody>
</table>
<p>二进制文件复制函数：</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;fstream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token keyword">static</span> <span class="token keyword">const</span> <span class="token keyword">int</span> BUFFERLEN<span class="token operator">=</span><span class="token number">2048</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> <span class="token function">copyFile</span><span class="token punctuation">(</span><span class="token keyword">const</span> std<span class="token double-colon punctuation">::</span>string <span class="token operator">&amp;</span>src<span class="token punctuation">,</span> <span class="token keyword">const</span> std<span class="token double-colon punctuation">::</span>string <span class="token operator">&amp;</span>dst<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">//打开源文件和目标文件</span>
    std<span class="token double-colon punctuation">::</span>ifstream <span class="token function">in</span><span class="token punctuation">(</span>src<span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> std<span class="token double-colon punctuation">::</span>ios<span class="token double-colon punctuation">::</span>in <span class="token operator">|</span> std<span class="token double-colon punctuation">::</span>ios<span class="token double-colon punctuation">::</span>binary<span class="token punctuation">)</span><span class="token punctuation">;</span>
    std<span class="token double-colon punctuation">::</span>ofstream <span class="token function">out</span><span class="token punctuation">(</span>dst<span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> std<span class="token double-colon punctuation">::</span>ios<span class="token double-colon punctuation">::</span>out <span class="token operator">|</span> std<span class="token double-colon punctuation">::</span>ios<span class="token double-colon punctuation">::</span>trunc<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//判断文件打开是否成功</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>in<span class="token operator">||</span><span class="token operator">!</span>out<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//从源文件读取数据写入到目标文件中</span>
    <span class="token comment">//通过读取源文件的EOF判断读写是否结束</span>
    <span class="token keyword">char</span> temp<span class="token punctuation">[</span>BUFFERLEN<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token operator">!</span>in<span class="token punctuation">.</span><span class="token function">eof</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        in<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>temp<span class="token punctuation">,</span>BUFFERLEN<span class="token punctuation">)</span><span class="token punctuation">;</span>
        std<span class="token double-colon punctuation">::</span>streamsize count<span class="token operator">=</span>in<span class="token punctuation">.</span><span class="token function">gcount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        out<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>temp<span class="token punctuation">,</span>count<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//关闭源文件与目标文件</span>
    in<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    out<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<hr>
<h3 id="STL">STL<a class="anchor" href="#STL">¶</a></h3>
<ul>
<li>STL(Standard Template Library)算法是泛型的(generic)，不与任何特定的数据结构和对象绑定，不必在环境类似的情况下重写代码；</li>
<li>STL算法可以量身定做，并且具有很好的效率；</li>
<li>STL可以进行扩展，你可以编写自己的组件并且能与STL标准的组件进行很好的配合；</li>
</ul>
<pre class="mermaid">graph LR
STL标准库六大组件--&gt;空间配置器 &amp; 容器 &amp; 适配器 &amp; 仿函数 &amp; 算法 &amp; 迭代器
空间配置器--&gt;allocator
容器--&gt;string &amp; vector &amp; list &amp; deque &amp; map &amp; set &amp; multimap &amp; multiset
适配器--&gt;stack &amp; queue &amp; priority_queue
仿函数--&gt;greater &amp; less
算法--&gt;find &amp; swap &amp; reverse &amp; sort &amp; merge
迭代器--&gt;iterator &amp; const_iterator &amp; reverse_iterator &amp; const_reverse_iterator</pre>
<h4 id="容器">容器<a class="anchor" href="#容器">¶</a></h4>
<p>容器用来存放数据，STL的容器分为两大类：</p>
<ul>
<li>序列式容器(Sequence Containers):其中的元素都是可排序的(ordered)，STL提供了<code>vector(单项开口的连续线性空间),list(环状双向列表),deque(双向开口的连续线性空间，并不是真正的连续线性空间，而是通过node_buffer模拟的)</code>等序列式容器，而<code>stack(栈，先进后出),queue(队列，先进先出),priority_queue(优先队列，按优先权)</code>则是容器适配器；</li>
<li>关联式容器(Associative Containers):每个数据元素都是由一个键(key)和值(value)组成，当元素被插入到容器时，按其键以某特定规则放入适当位置；常见的STL关联容器如:<code>set,muitiset,map(map.insert(pair&lt;type1,type2&gt;(value1,value2))或map.insert(map&lt;type1,type2&gt;::value_type(value1,value2)),multimap</code>；map中直接插入key一样的键值对会失败，数据不会被更新，但是用键进行修改则可以。</li>
</ul>
<h5 id="unordered-set">unordered_set<a class="anchor" href="#unordered-set">¶</a></h5>
<p>unodered_set是一种关联式容器，set和map内部实现是基于RB-Tree,有序，unordered_set和unordered_map是基于hashmap的，无序。</p>
<p><strong>使用时注意迭代器失效的问题。</strong></p>
<h4 id="仿函数">仿函数<a class="anchor" href="#仿函数">¶</a></h4>
<ul>
<li>仿函数(functor)一般不会单独使用，主要是为了搭配STL算法使用；</li>
<li>函数指针不能满足STL对抽象性的要求，不能满足软件积木的要求，无法和STL其他组件搭配；</li>
<li>本质就是类重载了operator()，创建一个行为类似函数的对象；</li>
</ul>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">//C++仿函数模板</span>
<span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">T</span><span class="token operator">&gt;</span>
<span class="token keyword">struct</span> <span class="token class-name">SortF</span><span class="token punctuation">{</span>
    <span class="token keyword">inline</span> <span class="token keyword">bool</span> <span class="token keyword">operator</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>T <span class="token keyword">const</span><span class="token operator">&amp;</span> a<span class="token punctuation">,</span>T <span class="token keyword">const</span><span class="token operator">&amp;</span> b<span class="token punctuation">)</span><span class="token keyword">const</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> a<span class="token operator">&lt;</span>b<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="算法">算法<a class="anchor" href="#算法">¶</a></h4>
<p>STL中的算法大致分为四类：包含<code>&lt;algorithm&gt;,&lt;numeric&gt;,&lt;functional&gt;</code></p>
<ol>
<li>非可变排序算法：指不直接修改其所操作的容器内容的算法；</li>
<li>可变排序算法：指可以修改它们所操作的容器内容的算法；</li>
<li>排序算法：包括对序列进行排序和合并的算法、搜索算法以及有序序列上的集合操作；</li>
<li>数值算法：对容器内容进行数值计算；</li>
</ol>
<h5 id="Lambda表达式">Lambda表达式<a class="anchor" href="#Lambda表达式">¶</a></h5>
<p>Lambda表达式完整的声明格式如下：</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token punctuation">[</span>capture list<span class="token punctuation">]</span> <span class="token punctuation">(</span>params list<span class="token punctuation">)</span> <span class="token keyword">mutable</span> exception<span class="token operator">-&gt;</span> <span class="token keyword">return</span> type <span class="token punctuation">{</span> function body <span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>各项具体含义如下：</p>
<ul>
<li>capture list：捕获外部变量列表</li>
<li>params list：形参列表</li>
<li>mutable指示符：用来说用是否可以修改捕获的变量</li>
<li>exception：异常设定</li>
<li>return type：返回类型</li>
<li>function body：函数体</li>
</ul>
<table>
<thead>
<tr>
<th>序号</th>
<th>格式</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>[capture list] (params list) -&gt; return type {function body}</td>
</tr>
<tr>
<td>2</td>
<td>[capture list] (params list) {function body}</td>
</tr>
<tr>
<td>3</td>
<td>[capture list] {function body}</td>
</tr>
</tbody>
</table>
<p>其中：</p>
<ul>
<li>格式1声明了const类型的表达式，这种类型的表达式不能修改捕获列表中的值。</li>
<li>格式2省略了返回值类型，但编译器可以根据以下规则推断出Lambda表达式的返回类型： （1）：如果function body中存在return语句，则该Lambda表达式的返回类型由return语句的返回类型确定； （2）：如果function body中没有return语句，则返回值为void类型。</li>
<li>格式3中省略了参数列表，类似普通函数中的无参函数。</li>
</ul>
<p><strong>对容器中的数进行操作可以考虑使用transform函数。</strong></p>
<p><strong>统计容器中有多少个该元素可以使用count函数。需要自己定义函数使用count_if函数，结合bind2nd和bind1st,类似<code>bind2nd(less&lt;int&gt;(),7)</code></strong></p>
<p>二分查找：<code>binary_search</code></p>
<p>查找子序列返回位置的地址：<code>search</code></p>
<hr>
<h3 id="设计模式">设计模式<a class="anchor" href="#设计模式">¶</a></h3>
<ul>
<li>23种面向对象的设计模式从分类上大致有创建型，结构型和行为型模式；</li>
<li>设计模式不是万能的，它建立在系统变化点上，哪里有变化哪里就可以用；</li>
<li>设计模式为了解耦合，为了扩展，它通常是演变过来的，需要演变才能准确定位；</li>
<li>设计模式是一种软件设计的方法，不是标准，当前大部分的框架中都已经包含了大量设计模式的思想；</li>
</ul>
<h4 id="单例模式-Singleton">单例模式(Singleton)<a class="anchor" href="#单例模式-Singleton">¶</a></h4>
<p>实现思路：</p>
<ol>
<li>Singleton拥有一个私有构造函数，确保用户无法通过new直接实例化。</li>
<li>包含一个静态私有成员变量instance与静态公有方法Instance()。</li>
</ol>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">//Singleton.h</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">ACWING_SINGLETON_H</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">ACWING_SINGLETON_H</span></span>


<span class="token keyword">class</span> <span class="token class-name">Singleton</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token keyword">static</span> <span class="token keyword">const</span> Singleton <span class="token operator">*</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//构造函数与析构函数私有化，防止外部访问</span>
<span class="token keyword">private</span><span class="token operator">:</span>
    <span class="token function">Singleton</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token operator">~</span><span class="token function">Singleton</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//使用静态变量帮助解决资源的分配和释放</span>
    <span class="token keyword">static</span> Singleton <span class="token operator">*</span>This<span class="token punctuation">;</span>

<span class="token punctuation">}</span><span class="token punctuation">;</span>


<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span> <span class="token comment">//ACWING_SINGLETON_H</span></span>
<span class="token comment">//Singleton.cpp</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">"Singleton.h"</span></span>
Singleton<span class="token operator">*</span> Singleton<span class="token double-colon punctuation">::</span>This<span class="token operator">=</span><span class="token keyword">new</span> <span class="token function">Singleton</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> Singleton <span class="token operator">*</span><span class="token class-name">Singleton</span><span class="token double-colon punctuation">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">nullptr</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token class-name">Singleton</span><span class="token double-colon punctuation">::</span><span class="token function">Singleton</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

<span class="token punctuation">}</span>

<span class="token class-name">Singleton</span><span class="token double-colon punctuation">::</span><span class="token operator">~</span><span class="token function">Singleton</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="观察者模式-Observer">观察者模式(Observer)<a class="anchor" href="#观察者模式-Observer">¶</a></h4>
<p>实现思路：</p>
<ol>
<li>将问题的指责解耦合，将Observable和Observer抽象开，分清抽象和实体。</li>
</ol>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">//Observer.h</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">ACWING_OBSERVER_H</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">ACWING_OBSERVER_H</span></span>

<span class="token keyword">class</span> <span class="token class-name">Observer</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token function">Observer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=</span> <span class="token keyword">default</span><span class="token punctuation">;</span>
    <span class="token keyword">virtual</span> <span class="token operator">~</span><span class="token function">Observer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=</span> <span class="token keyword">default</span><span class="token punctuation">;</span>
    <span class="token comment">//当被观察对象发生变化时，通知被观察者调用这个方法</span>
    <span class="token keyword">virtual</span> <span class="token keyword">void</span> <span class="token function">Update</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span> pArg<span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span> <span class="token comment">//ACWING_OBSERVER_H</span></span>

<span class="token comment">//Observable.h</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">ACWING_OBSERVABLE_H</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">ACWING_OBSERVABLE_H</span></span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;list&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;string&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">"Observer.h"</span></span>

<span class="token keyword">class</span> <span class="token class-name">Observable</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token function">Observable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">virtual</span> <span class="token operator">~</span><span class="token function">Observable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token keyword">default</span><span class="token punctuation">;</span>

    <span class="token comment">//注册观察者</span>
    <span class="token keyword">void</span> <span class="token function">Attach</span><span class="token punctuation">(</span>Observer <span class="token operator">*</span>pOb<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//注销观察者</span>
    <span class="token keyword">void</span> <span class="token function">Detach</span><span class="token punctuation">(</span>Observer <span class="token operator">*</span>pOb<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">int</span> <span class="token function">GetObserverCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">const</span><span class="token punctuation">;</span>

    <span class="token keyword">void</span> <span class="token function">DetachAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        _Obs<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">virtual</span> <span class="token keyword">void</span> <span class="token function">GetSomeNews</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>string str<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">SetChange</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

<span class="token keyword">protected</span><span class="token operator">:</span>
    <span class="token keyword">void</span> <span class="token function">SetChange</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>string str<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">private</span><span class="token operator">:</span>
    std<span class="token double-colon punctuation">::</span>list<span class="token operator">&lt;</span>Observer <span class="token operator">*</span><span class="token operator">&gt;</span> _Obs<span class="token punctuation">;</span>
    <span class="token keyword">bool</span> _bChange<span class="token punctuation">;</span>
    <span class="token comment">//通知函数</span>
    <span class="token keyword">void</span> <span class="token function">Notify</span><span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span>pArg<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span> <span class="token comment">//ACWING_OBSERVABLE_H</span></span>

<span class="token comment">//Observable.cpp</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">"Observable.h"</span></span>

<span class="token class-name">Observable</span><span class="token double-colon punctuation">::</span><span class="token function">Observable</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token function">_bChange</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

<span class="token keyword">void</span> <span class="token class-name">Observable</span><span class="token double-colon punctuation">::</span><span class="token function">Attach</span><span class="token punctuation">(</span>Observer <span class="token operator">*</span>pOb<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>pOb<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">auto</span> it <span class="token operator">=</span> _Obs<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token punctuation">;</span> it <span class="token operator">!=</span> _Obs<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">*</span>it <span class="token operator">==</span> pOb<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    _Obs<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>pOb<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">void</span> <span class="token class-name">Observable</span><span class="token double-colon punctuation">::</span><span class="token function">Detach</span><span class="token punctuation">(</span>Observer <span class="token operator">*</span>pOb<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>pOb <span class="token operator">||</span> _Obs<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    _Obs<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>pOb<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token class-name">Observable</span><span class="token double-colon punctuation">::</span><span class="token function">GetObserverCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">const</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> _Obs<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">void</span> <span class="token class-name">Observable</span><span class="token double-colon punctuation">::</span><span class="token function">Notify</span><span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span>pArg<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>_bChange<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">auto</span> it <span class="token operator">=</span> _Obs<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token punctuation">;</span> it <span class="token operator">!=</span> _Obs<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token punctuation">(</span><span class="token operator">*</span>it<span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">Update</span><span class="token punctuation">(</span>pArg<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    _bChange <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">void</span> <span class="token class-name">Observable</span><span class="token double-colon punctuation">::</span><span class="token function">SetChange</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>string str<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    _bChange <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    <span class="token function">Notify</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span> str<span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">//main.cpp</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">"Observable.h"</span></span>

<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">News</span><span class="token operator">:</span> <span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">Observable</span></span><span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token keyword">virtual</span> <span class="token keyword">void</span> <span class="token function">GetSomeNews</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>string str<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">SetChange</span><span class="token punctuation">(</span><span class="token string">"News: "</span><span class="token operator">+</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">User1</span><span class="token operator">:</span> <span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">Observer</span></span><span class="token punctuation">{</span>
    <span class="token keyword">void</span> <span class="token function">Update</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span> pArg<span class="token punctuation">)</span> <span class="token keyword">override</span><span class="token punctuation">{</span>
        cout<span class="token operator">&lt;&lt;</span><span class="token string">"User1 Got News"</span><span class="token operator">&lt;&lt;</span><span class="token string">", "</span><span class="token operator">&lt;&lt;</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token punctuation">)</span>pArg<span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">User2</span><span class="token operator">:</span> <span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">Observer</span></span><span class="token punctuation">{</span>
    <span class="token keyword">void</span> <span class="token function">Update</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token operator">*</span> pArg<span class="token punctuation">)</span> <span class="token keyword">override</span><span class="token punctuation">{</span>
        cout<span class="token operator">&lt;&lt;</span><span class="token string">"User2 Got News"</span><span class="token operator">&lt;&lt;</span><span class="token string">", "</span><span class="token operator">&lt;&lt;</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token punctuation">)</span>pArg<span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    User1 user1<span class="token punctuation">;</span>
    User2 user2<span class="token punctuation">;</span>
    News n1<span class="token punctuation">;</span>
    n1<span class="token punctuation">.</span><span class="token function">Attach</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user1<span class="token punctuation">)</span><span class="token punctuation">;</span>
    n1<span class="token punctuation">.</span><span class="token function">Attach</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user2<span class="token punctuation">)</span><span class="token punctuation">;</span>
    n1<span class="token punctuation">.</span><span class="token function">GetSomeNews</span><span class="token punctuation">(</span><span class="token string">"hello world"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    cout<span class="token operator">&lt;&lt;</span>n1<span class="token punctuation">.</span><span class="token function">GetObserverCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
    n1<span class="token punctuation">.</span><span class="token function">Detach</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user2<span class="token punctuation">)</span><span class="token punctuation">;</span>
    n1<span class="token punctuation">.</span><span class="token function">GetSomeNews</span><span class="token punctuation">(</span><span class="token string">"hello world"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    cout<span class="token operator">&lt;&lt;</span>n1<span class="token punctuation">.</span><span class="token function">GetObserverCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
    n1<span class="token punctuation">.</span><span class="token function">DetachAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    n1<span class="token punctuation">.</span><span class="token function">GetSomeNews</span><span class="token punctuation">(</span><span class="token string">"hello world"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    cout<span class="token operator">&lt;&lt;</span>n1<span class="token punctuation">.</span><span class="token function">GetObserverCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="适配器模式-Adapter">适配器模式(Adapter)<a class="anchor" href="#适配器模式-Adapter">¶</a></h4>
<ul>
<li>适配器将类接口转换为客户端期望的另一个接口</li>
<li>使用适配器可防止类由于接口不兼容而不工作</li>
<li>适配器模式的动机是，如果可以更改接口，则可以重用现有软件</li>
</ul>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">//LegacyRectanle.h</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">ACWING_LEGACYRECTANLE_H</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">ACWING_LEGACYRECTANLE_H</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token comment">//原始数据类</span>
<span class="token keyword">class</span> <span class="token class-name">LegacyRectanle</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token function">LegacyRectanle</span><span class="token punctuation">(</span><span class="token keyword">double</span> x1<span class="token punctuation">,</span><span class="token keyword">double</span> y1<span class="token punctuation">,</span><span class="token keyword">double</span> x2<span class="token punctuation">,</span><span class="token keyword">double</span> y2<span class="token punctuation">)</span><span class="token operator">:</span><span class="token function">_x1</span><span class="token punctuation">(</span>x1<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">_y1</span><span class="token punctuation">(</span>y1<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">_x2</span><span class="token punctuation">(</span>x2<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">_y2</span><span class="token punctuation">(</span>y2<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token keyword">void</span> <span class="token function">LagacyRectangleDraw</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">private</span><span class="token operator">:</span>
    <span class="token keyword">double</span> _x1<span class="token punctuation">,</span>_y1<span class="token punctuation">,</span>_x2<span class="token punctuation">,</span>_y2<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token comment">//接口调用类</span>
<span class="token keyword">class</span> <span class="token class-name">Rectangle</span><span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token keyword">virtual</span> <span class="token keyword">void</span> <span class="token function">Draw</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>string str<span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token comment">//第一种适配方式：使用多重继承</span>
<span class="token keyword">class</span> <span class="token class-name">RectangleAdapter</span><span class="token operator">:</span> <span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">Rectangle</span><span class="token punctuation">,</span> <span class="token keyword">public</span> <span class="token class-name">LegacyRectanle</span></span><span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token function">RectangleAdapter</span><span class="token punctuation">(</span><span class="token keyword">double</span> x<span class="token punctuation">,</span><span class="token keyword">double</span> y<span class="token punctuation">,</span><span class="token keyword">double</span> w<span class="token punctuation">,</span><span class="token keyword">double</span> h<span class="token punctuation">)</span><span class="token operator">:</span><span class="token function">LegacyRectanle</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span>x<span class="token operator">+</span>w<span class="token punctuation">,</span>y<span class="token operator">+</span>h<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token keyword">virtual</span> <span class="token keyword">void</span> <span class="token function">Draw</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>string str<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token comment">//第二种适配方式：使用组合</span>
<span class="token keyword">class</span> <span class="token class-name">RectangleAdapter2</span><span class="token operator">:</span> <span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">Rectangle</span></span><span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token function">RectangleAdapter2</span><span class="token punctuation">(</span><span class="token keyword">double</span> x<span class="token punctuation">,</span><span class="token keyword">double</span> y<span class="token punctuation">,</span><span class="token keyword">double</span> w<span class="token punctuation">,</span><span class="token keyword">double</span> h<span class="token punctuation">)</span><span class="token operator">:</span><span class="token function">_legacyRectanle</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span>x<span class="token operator">+</span>w<span class="token punctuation">,</span>y<span class="token operator">+</span>h<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token keyword">virtual</span> <span class="token keyword">void</span> <span class="token function">Draw</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>string str<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">private</span><span class="token operator">:</span>
    LegacyRectanle _legacyRectanle<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span> <span class="token comment">//ACWING_LEGACYRECTANLE_H</span></span>

<span class="token comment">//LegacyRectanle.cpp</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">"LegacyRectanle.h"</span></span>
<span class="token keyword">void</span> <span class="token class-name">LegacyRectanle</span><span class="token double-colon punctuation">::</span><span class="token function">LagacyRectangleDraw</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    std<span class="token double-colon punctuation">::</span>cout<span class="token operator">&lt;&lt;</span>_x1<span class="token operator">&lt;&lt;</span>_y1<span class="token operator">&lt;&lt;</span>_x2<span class="token operator">&lt;&lt;</span>_y2<span class="token operator">&lt;&lt;</span>std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">void</span> <span class="token class-name">RectangleAdapter</span><span class="token double-colon punctuation">::</span><span class="token function">Draw</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>string str<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">LagacyRectangleDraw</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    std<span class="token double-colon punctuation">::</span>cout<span class="token operator">&lt;&lt;</span>str<span class="token operator">&lt;&lt;</span>std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">void</span> <span class="token class-name">RectangleAdapter2</span><span class="token double-colon punctuation">::</span><span class="token function">Draw</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>string str<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    _legacyRectanle<span class="token punctuation">.</span><span class="token function">LagacyRectangleDraw</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    std<span class="token double-colon punctuation">::</span>cout<span class="token operator">&lt;&lt;</span>str<span class="token operator">&lt;&lt;</span>std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">"LegacyRectanle.h"</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">double</span> x<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span>y<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">,</span>w<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>h<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">;</span>
    Rectangle<span class="token operator">*</span> a<span class="token operator">=</span><span class="token generic-function"><span class="token function">dynamic_cast</span><span class="token generic class-name"><span class="token operator">&lt;</span>Rectangle<span class="token operator">*</span><span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token function">RectangleAdapter</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span>w<span class="token punctuation">,</span>h<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    a<span class="token operator">-&gt;</span><span class="token function">Draw</span><span class="token punctuation">(</span><span class="token string">"hello"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    Rectangle<span class="token operator">*</span> b<span class="token operator">=</span><span class="token generic-function"><span class="token function">dynamic_cast</span><span class="token generic class-name"><span class="token operator">&lt;</span>Rectangle<span class="token operator">*</span><span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token function">RectangleAdapter2</span><span class="token punctuation">(</span>x<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>y<span class="token operator">+</span><span class="token number">2</span><span class="token punctuation">,</span>w<span class="token punctuation">,</span>h<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    b<span class="token operator">-&gt;</span><span class="token function">Draw</span><span class="token punctuation">(</span><span class="token string">"hello"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<hr>
<h3 id="Tip">Tip<a class="anchor" href="#Tip">¶</a></h3>
<p><code>sizeof(char str[])</code>为字面值+1。</p>
<h4 id="浅拷贝与深拷贝">浅拷贝与深拷贝<a class="anchor" href="#浅拷贝与深拷贝">¶</a></h4>
<p>浅拷贝：只拷贝指针地址，C++默认拷贝构造函数与赋值运算符重载都是浅拷贝；节省空间，但是容易引发多次释放。</p>
<p>深拷贝：重新分配堆内存，拷贝指针指向内容。浪费空间，但不会导致多次释放。</p>
<p>为了兼顾着浅拷贝和深拷贝的优点，解决方案有两种：</p>
<ul>
<li>引用计数</li>
<li>C++11的移动语义</li>
</ul>
<h4 id="noexcept">noexcept<a class="anchor" href="#noexcept">¶</a></h4>
<p>该关键字告诉编译器，函数中不会发生异常,这有利于编译器对程序做更多的优化。</p>
<p>如果在运行时，<code>oexecpt</code>函数向外抛出了异常（如果函数内部捕捉了异常并完成处理，这种情况不算抛出异常），程序会直接终止，调用std::terminate()函数，该函数内部会调用std::abort()终止程序。</p>
<h5 id="C-的异常处理">C++的异常处理<a class="anchor" href="#C-的异常处理">¶</a></h5>
<p>C++的异常处理是在运行时而不是编译期检测的。为了实现运行时检测，编译器创建额外的代码，然而这会妨碍程序优化。在实践中，一般两种异常抛出方式是最常用的：</p>
<ul>
<li>一个操作或函数可能抛出一个异常；</li>
<li>一个操作或函数不可能抛出一个异常；</li>
</ul>
<p>后面这种情况在以往的C<ins>中常用throw()表示，在C</ins>11中已经被<code>noexcept</code>替代。</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">//C++11之前</span>
<span class="token keyword">void</span> <span class="token function">swap</span><span class="token punctuation">(</span>Type<span class="token operator">&amp;</span> x<span class="token punctuation">,</span>Type<span class="token operator">&amp;</span> y<span class="token punctuation">)</span> <span class="token keyword">throw</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    x<span class="token punctuation">.</span><span class="token function">swap</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//C++11</span>
<span class="token keyword">void</span> <span class="token function">swap</span><span class="token punctuation">(</span>Type<span class="token operator">&amp;</span> x<span class="token punctuation">,</span>Type<span class="token operator">&amp;</span> y<span class="token punctuation">)</span> <span class="token keyword">noexcept</span><span class="token punctuation">{</span>
    x<span class="token punctuation">.</span><span class="token function">swap</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>更加灵活的使用<code>noexcept</code>：</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">void</span> <span class="token function">swap</span><span class="token punctuation">(</span>Type<span class="token operator">&amp;</span> x<span class="token punctuation">,</span> Type<span class="token operator">&amp;</span> y<span class="token punctuation">)</span> <span class="token keyword">noexcept</span><span class="token punctuation">(</span><span class="token keyword">noexcept</span><span class="token punctuation">(</span>x<span class="token punctuation">.</span><span class="token function">swap</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        x<span class="token punctuation">.</span><span class="token function">swap</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<p>它表示，如果操作<code>x.swap(y)</code>不发生异常，那么函数<code>swap(Type&amp; x, Type&amp; y)</code>一定不发生异常。</p>
<p>并不是加上<code>noexcept</code>就能提高效率，以下场景鼓励使用<code>noexcept</code>：</p>
<ul>
<li>移动构造函数</li>
<li>移动分配函数</li>
<li>析构函数(新版本的编译器中，析构函数是默认添加关键字<code>noexcept</code>)</li>
<li>叶子函数(叶子函数：指在寒回溯内部不分配栈空间，也不调用其他函数，也不存储非易失性寄存器，也不处理异常)</li>
</ul>
<p>除此以外，不推荐使用<code>noexcept</code>。</p>
<h4 id="default和-delete">=default和=delete<a class="anchor" href="#default和-delete">¶</a></h4>
<p><code>=delete</code>和<code>=default</code>是c++11的新特性，分别是显式删除（告知编译器<strong>不生成</strong>函数默认的缺省版本）和显式缺省（告诉编译器<strong>生成</strong>函数默认的缺省版本），这样可以增强对“类默认函数的控制”。</p>
<h5 id="类与默认函数">类与默认函数<a class="anchor" href="#类与默认函数">¶</a></h5>
<p>当我们编写一个类的时候，若不显著写明，则类会默认为我们提供几个函数与全局默认操作符：</p>
<ul>
<li>构造函数</li>
<li>析构函数</li>
<li>拷贝构造函数</li>
<li>拷贝赋值函数（operator=）</li>
<li>移动构造函数</li>
<li>operator,</li>
<li>operator&amp;</li>
<li>operator*</li>
<li>operator-&gt;</li>
<li>operator-&gt;*</li>
<li>operator new</li>
<li>operator delete</li>
</ul>
<p>使用&lt;类&gt;(){}这样的方式来声明无参构造函数会带来一个问题，使得其不再是POD类型。</p>
<h4 id="List">List<a class="anchor" href="#List">¶</a></h4>
<p>List将元素按顺序存储在链表中，与向量（vectors）相比，它允许快速的插入和删除，但随机访问比较慢。</p>
<table>
<thead>
<tr>
<th>函数</th>
<th>作用</th>
</tr>
</thead>
<tbody>
<tr>
<td>assign()</td>
<td>给list赋值</td>
</tr>
<tr>
<td>back()</td>
<td>返回最后一个元素</td>
</tr>
<tr>
<td>begin()</td>
<td>返回指向第一个元素的迭代器</td>
</tr>
<tr>
<td>clear()</td>
<td>删除所有元素</td>
</tr>
<tr>
<td>empty()</td>
<td>如果list是空的返回true</td>
</tr>
<tr>
<td>end()</td>
<td>返回末尾的迭代器</td>
</tr>
<tr>
<td>erase()</td>
<td>删除一个元素</td>
</tr>
<tr>
<td>front()</td>
<td>返回第一个元素</td>
</tr>
<tr>
<td>get_allocator()</td>
<td>返回list的配置器</td>
</tr>
<tr>
<td>insert()</td>
<td>插入一个元素到list中</td>
</tr>
<tr>
<td>max_size()</td>
<td>返回list能容纳的最大元素数量</td>
</tr>
<tr>
<td>merge()</td>
<td>合并两个list</td>
</tr>
<tr>
<td>pop_back()</td>
<td>删除最后一个元素</td>
</tr>
<tr>
<td>pop_front()</td>
<td>删除第一个元素</td>
</tr>
<tr>
<td>push_back()</td>
<td>在list的末尾添加一个元素</td>
</tr>
<tr>
<td>push_front()</td>
<td>在list的头部添加一个元素</td>
</tr>
<tr>
<td>rbegin()</td>
<td>返回指向第一个元素的逆向迭代器</td>
</tr>
<tr>
<td>remove()</td>
<td>从list中删除元素</td>
</tr>
<tr>
<td>remove_if()</td>
<td>按指定条件删除元素</td>
</tr>
<tr>
<td>rend()</td>
<td>指向list末尾的逆向迭代器</td>
</tr>
<tr>
<td>resize()</td>
<td>改变list的大小</td>
</tr>
<tr>
<td>reverse()</td>
<td>把list中的元素倒转</td>
</tr>
<tr>
<td>size()</td>
<td>返回list中的元素个数</td>
</tr>
<tr>
<td>sort()</td>
<td>给list排序</td>
</tr>
<tr>
<td>splice()</td>
<td>合并两个list</td>
</tr>
<tr>
<td>swap()</td>
<td>交换两个list</td>
</tr>
<tr>
<td>unique()</td>
<td>删除list中的重复元素</td>
</tr>
</tbody>
</table>
<h4 id="POD">POD<a class="anchor" href="#POD">¶</a></h4>
<p><code>POD</code>是Plain Old Data的缩写，是C++定义的一类数据结构。Plain表示它是一个普通类型，Old表示它是旧的与几十年前的C语言兼容，可以使用memcpy()这种最原始的函数进行操作。两个系统进行交换数据，如果没有办法对数据进行语义检查和解释，那就只能以非常底层的数据形式进行交互，而拥有 POD 特征的类或者结构体通过二进制拷贝后依然能保持数据结构不变。也就是说，<strong>能用 C 的 <code>memcpy() </code>等函数进行操作的类、结构体就是 POD 类型的数据。</strong></p>
<h5 id="POD特征">POD特征<a class="anchor" href="#POD特征">¶</a></h5>
<p>是不是 POD 类型的，可以用 <code>is_pod&lt;T&gt;::value </code>来判断。那什么样的类、结构体是拥有 POD 特性的呢？要求有两个：<strong>一个是它必须很平凡、很普通；另一个是布局有序</strong>。</p>
<ul>
<li>
<p><strong>能平凡就平凡</strong>，<strong>不能写析构函数/构造函数、拷贝/移动构造函数、拷贝/移动运算符，不能有虚函数与虚基类</strong>，而是用编译期自动生成的。那“这个数据就是平凡的”，非要写的话，使用C++11的default关键字。如下所示：</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">A</span><span class="token punctuation">{</span>
    <span class="token function">A</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">B</span><span class="token punctuation">{</span>
    
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">C</span><span class="token punctuation">{</span>
    <span class="token function">C</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token keyword">default</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    cout<span class="token operator">&lt;&lt;</span>std<span class="token double-colon punctuation">::</span>is_trivial<span class="token operator">&lt;</span>A<span class="token operator">&gt;</span><span class="token double-colon punctuation">::</span>value<span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
    cout<span class="token operator">&lt;&lt;</span>std<span class="token double-colon punctuation">::</span>is_trivial<span class="token operator">&lt;</span>B<span class="token operator">&gt;</span><span class="token double-colon punctuation">::</span>value<span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
    cout<span class="token operator">&lt;&lt;</span>std<span class="token double-colon punctuation">::</span>is_trivial<span class="token operator">&lt;</span>C<span class="token operator">&gt;</span><span class="token double-colon punctuation">::</span>value<span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// 输出</span>
<span class="token number">0</span>
<span class="token number">1</span>
<span class="token number">1</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</li>
<li>
<p><strong>布局有序，普通成员有相同的访问级别。注意是普通成员。静态成员之类的不在此考虑范围。</strong></p>
</li>
<li>
<p><strong>类中的第一个非静态成员的类型不能与其基类相同，不然就不是标准布局的类型。</strong></p>
</li>
<li>
<p><strong>只要有父类，普通成员只能在其中一个类中，不可分散。</strong></p>
</li>
</ul>
<h5 id="使用POD数据">使用POD数据<a class="anchor" href="#使用POD数据">¶</a></h5>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">A</span><span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token keyword">int</span> a<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//初始化a1</span>
    A a1<span class="token punctuation">;</span>
    a1<span class="token punctuation">.</span>a<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">;</span>
    <span class="token comment">//使用C语言的memcpy()底层函数拷贝数据到p中</span>
    <span class="token keyword">char</span><span class="token operator">*</span> p<span class="token operator">=</span><span class="token keyword">new</span> <span class="token keyword">char</span><span class="token punctuation">[</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>A<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token function">memcpy</span><span class="token punctuation">(</span>p<span class="token punctuation">,</span><span class="token operator">&amp;</span>a1<span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>A<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//将p强制转换成A*</span>
    A<span class="token operator">*</span> a2<span class="token operator">=</span><span class="token generic-function"><span class="token function">reinterpret_cast</span><span class="token generic class-name"><span class="token operator">&lt;</span>A<span class="token operator">*</span><span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span>p<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="void-NULL-nullptr">void*,NULL,nullptr<a class="anchor" href="#void-NULL-nullptr">¶</a></h4>
<p>在C中<code>#define NULL ((void *)0)</code>，其中<code>void *</code>可以表示所有指针。</p>
<p>在C++中定义如下：</p>
<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression"><span class="token constant">NULL</span></span></span>
	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">__cplusplus</span></span>
		<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">NULL</span> <span class="token expression"><span class="token number">0</span></span></span>
	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">else</span></span>
		<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">NULL</span> <span class="token expression"><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token number">0</span><span class="token punctuation">)</span></span></span>
	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span> </span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>在C++11中，nullptr用来代表(void *)0,NULL则表示为0；</p>
<h4 id="类型转换">类型转换<a class="anchor" href="#类型转换">¶</a></h4>
<p>提供给开发者用来进行指针和引用的转换。</p>
<ul>
<li>const_cast&lt;new_type&gt;(expression):用于转换指针或引用，去掉类型的const属性。但是其原本的数值并不会改变，只是一种处于无奈的情况。或者一个修饰了const属性的指针或引用指向的是一个非const修饰的数据时，才可以修改原数值。</li>
<li>static_cast&lt;new_type&gt;(expression):用于基本类型转换，有继承关系类对象和类指针之间转换，由程序员来确保转换是安全的，它不会产生动态转换的类型安全检查的开销。</li>
<li>reinterpret_cast&lt;new_type&gt;(expression):<strong>很危险</strong>，重新解释类型，既不检查指向的内容，也不检查指针类型本身，但要求转换前后的类型所占用空间大小一致，否则引发编译时错误。</li>
<li>dynamic_cast&lt;new_type&gt;(expression):只能用于含有虚函数的类，必须用在多态体系中，用于类层次间的向上和向下转换；向下转化时，如果是非法的对于指针返回NULL，需要做判空处理。</li>
</ul>
<h4 id="和-区别">-&gt;和.区别<a class="anchor" href="#和-区别">¶</a></h4>
<p>-&gt;左侧是指针，.左侧是实体。</p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">不二</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://buerlog.top/2020/11/10/cheng-xu-she-ji/cpp/cpp-fu-xi-bi-ji/">http://buerlog.top/2020/11/10/cheng-xu-she-ji/cpp/cpp-fu-xi-bi-ji/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">不二</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/Cpp/">
                                    <span class="chip bg-color">Cpp</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2020/11/18/qian-ru-shi/esp32-zhi-wen-lan-ya-jian-pan-kai-fa/">
                    <div class="card-image">
                        
                        <img src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@src/source/_posts/PageImg/嵌入式/nnn.jpg" class="responsive-img" alt="Esp32指纹蓝牙键盘开发">
                        
                        <span class="card-title">Esp32指纹蓝牙键盘开发</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2020-11-18
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F/" class="post-category">
                                    嵌入式
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/%E6%A0%91%E8%8E%93%E6%B4%BE/">
                        <span class="chip bg-color">树莓派</span>
                    </a>
                    
                    <a href="/tags/Esp32/">
                        <span class="chip bg-color">Esp32</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2020/11/10/suan-fa/leetcode-he-ji/leetcode-shua-ti-ji-lu/">
                    <div class="card-image">
                        
                        <img src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@src/source/_posts/PageImg/算法/leetcode.png" class="responsive-img" alt="Leetcode刷题记录">
                        
                        <span class="card-title">Leetcode刷题记录</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2020-11-10
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E7%AE%97%E6%B3%95/" class="post-category">
                                    算法
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Cpp/">
                        <span class="chip bg-color">Cpp</span>
                    </a>
                    
                    <a href="/tags/Leetcode/">
                        <span class="chip bg-color">Leetcode</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>


<script>
    $('#articleContent').on('copy', function (e) {
        // IE8 or earlier browser is 'undefined'
        if (typeof window.getSelection === 'undefined') return;

        var selection = window.getSelection();
        // if the selection is short let's not annoy our users.
        if (('' + selection).length < Number.parseInt('120')) {
            return;
        }

        // create a div outside of the visible area and fill it with the selected text.
        var bodyElement = document.getElementsByTagName('body')[0];
        var newdiv = document.createElement('div');
        newdiv.style.position = 'absolute';
        newdiv.style.left = '-99999px';
        bodyElement.appendChild(newdiv);
        newdiv.appendChild(selection.getRangeAt(0).cloneContents());

        // we need a <pre> tag workaround.
        // otherwise the text inside "pre" loses all the line breaks!
        if (selection.getRangeAt(0).commonAncestorContainer.nodeName === 'PRE') {
            newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
        }

        var url = document.location.href;
        newdiv.innerHTML += '<br />'
            + '来源: 不二博客<br />'
            + '文章作者: 不二<br />'
            + '文章链接: <a href="' + url + '">' + url + '</a><br />'
            + '本文章著作权归作者所有，任何形式的转载都请注明出处。';

        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {bodyElement.removeChild(newdiv);}, 200);
    });
</script>


<!-- 代码块功能依赖 -->
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4, h5, h6'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4, h5, h6').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="song"
                   id="1901371647"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

    
    <div class="container row center-align" style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2018-2023</span>
            
            <span id="year">2018</span>
            <a href="/about" target="_blank">不二</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                class="white-color">263.4k</span>&nbsp;字
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <span id="sitetime">载入运行时间...</span>
            <script>
                function siteTime() {
                    var seconds = 1000;
                    var minutes = seconds * 60;
                    var hours = minutes * 60;
                    var days = hours * 24;
                    var years = days * 365;
                    var today = new Date();
                    var startYear = "2018";
                    var startMonth = "5";
                    var startDate = "25";
                    var startHour = "17";
                    var startMinute = "20";
                    var startSecond = "53";
                    var todayYear = today.getFullYear();
                    var todayMonth = today.getMonth() + 1;
                    var todayDate = today.getDate();
                    var todayHour = today.getHours();
                    var todayMinute = today.getMinutes();
                    var todaySecond = today.getSeconds();
                    var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                    var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                    var diff = t2 - t1;
                    var diffYears = Math.floor(diff / years);
                    var diffDays = Math.floor((diff / days) - diffYears * 365);
                    var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
                    var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) /
                        minutes);
                    var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours -
                        diffMinutes * minutes) / seconds);
                    if (startYear == todayYear) {
                        document.getElementById("year").innerHTML = todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffDays + " 天 " + diffHours +
                            " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    } else {
                        document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffYears + " 年 " + diffDays +
                            " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    }
                }
                setInterval(siteTime, 1000);
            </script>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/weiyouwozuiku" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:weiyouwozuiku@gmail.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=1104891151" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 1104891151" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>





    <a href="https://www.zhihu.com/people/he-he-9-35-47" class="tooltipped" target="_blank" data-tooltip="关注我的知乎: https://www.zhihu.com/people/he-he-9-35-47" data-position="top" data-delay="50">
        <i class="fab fa-zhihu1">知</i>
    </a>



    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>

    <script src='https://unpkg.com/mermaid@8.14.0/dist/mermaid.min.js'></script>
    <script>
      if (window.mermaid) {
        mermaid.initialize({theme: 'forest'});
      }
    </script>
  

    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/materialize/materialize.min.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/aos/aos.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/js/matery.js"></script>

    <!-- Baidu Analytics -->

<script>
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?943a68f473d421f2d98228f74edc63d0";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    
    <script async src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    
    <script type="text/javascript" color="0,0,255"
        pointColor="0,0,255" opacity='0.7'
        zIndex="-1" count="150"
        src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/background/canvas-nest.js"></script>
    

    
    
    <script type="text/javascript" size="150" alpha='0.6'
        zIndex="-1" src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/background/ribbon-refresh.min.js" async="async"></script>
    

    
    <script type="text/javascript" src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/background/ribbon-dynamic.js" async="async"></script>
    

    
    <script src="https://cdn.jsdelivr.net/gh/weiyouwozuiku/weiyouwozuiku.github.io@master/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
